package com.h2psoft.truyen.tranh.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.h2psoft.truyen.tranh.R;
import com.h2psoft.truyen.tranh.activities.ActivityComicInfo;
import com.h2psoft.truyen.tranh.constants.Constants;
import com.h2psoft.truyen.tranh.objects.ObjComic;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

public class AdapterListComic extends BaseAdapter {
	private Activity act;
	private ArrayList<ObjComic> arrComic;
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private Intent intent;
	private ImageLoaderConfiguration imageConfiguration;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;

	public AdapterListComic(Activity act, ArrayList<ObjComic> arrComic) {
		this.act = act;
		this.arrComic = arrComic;
		initView();
		initImageLoader();
	}

	private void initView() {
		inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	private void initImageLoader() {
		options = new DisplayImageOptions.Builder().cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY_STRETCHED).resetViewBeforeLoading()
				.cacheOnDisc().bitmapConfig(Bitmap.Config.RGB_565).build();
		imageConfiguration = new ImageLoaderConfiguration.Builder(act).threadPoolSize(2).threadPriority(2).defaultDisplayImageOptions(options)
				.memoryCacheExtraOptions(100, 100).build();
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(imageConfiguration);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.adapter_list_comic, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.imgComicIcon = (ImageView) convertView.findViewById(R.id.imgIco_listComic);
			viewHolder.tvwComicCount = (TextView) convertView.findViewById(R.id.tvwComicCount_listComic);
			viewHolder.tvwComicStatic = (TextView) convertView.findViewById(R.id.tvwComicStatic_listComic);
			viewHolder.tvwComicTitle = (TextView) convertView.findViewById(R.id.tvwComicTitle_listComic);
			viewHolder.lnlComicItem = (LinearLayout) convertView.findViewById(R.id.lnlComicItem);
			convertView.setTag(viewHolder);
		} else
			viewHolder = (ViewHolder) convertView.getTag();
		// if (position == 0)
		// viewHolder.imgComicIcon.setImageResource(R.drawable.kimchi_cucai);
		// if (position == 1)
		// viewHolder.imgComicIcon.setImageResource(R.drawable.quocxeng);
		ObjComic objComic = arrComic.get(position);
		imageLoader.displayImage(objComic.getIconUrl(), viewHolder.imgComicIcon);
		viewHolder.tvwComicTitle.setText(objComic.getName());
		viewHolder.tvwComicStatic.setText(" " + objComic.getStatus());
		viewHolder.tvwComicCount.setText(" " + objComic.getCount());
		viewHolder.lnlComicItem.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				putExtra(arrComic.get(position).getId(), arrComic.get(position).getCount(), arrComic.get(position).getName(), arrComic.get(position)
						.getAuthor(), arrComic.get(position).getStatus());
			}
		});
		viewHolder.imgComicIcon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				putExtra(arrComic.get(position).getId(), arrComic.get(position).getCount(), arrComic.get(position).getName(), arrComic.get(position)
						.getAuthor(), arrComic.get(position).getStatus());
			}
		});
		return convertView;
	}

	private void putExtra(int comicId, int comicCount, String comicName, String comicEdior, String comicStatus) {
		intent = new Intent(act, ActivityComicInfo.class);
		intent.putExtra(Constants.KEY_COMIC_ID, comicId);
		intent.putExtra(Constants.KEY_COMIC_COUNT, comicCount);
		intent.putExtra(Constants.KEY_COMIC_NAME, comicName);
		intent.putExtra(Constants.KEY_COMIC_EDITOR, comicEdior);
		intent.putExtra(Constants.KEY_COMIC_STATUS, comicStatus);
		act.startActivity(intent);
		act.finish();
	}

	class ViewHolder {
		LinearLayout lnlComicItem;
		ImageView imgComicIcon;
		TextView tvwComicTitle, tvwComicStatic, tvwComicCount, tvwComicAuthor;
	}

	@Override
	public int getCount() {
		return arrComic.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arrComic.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
}